<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable>
          <el-option
            v-for="dict in dict.type.pay_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付类型" clearable>
          <el-option
            v-for="dict in dict.type.pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
       <el-button icon="el-icon-connection" size="mini"  @click="summaryShow" v-if="!isShow">统计汇总展示</el-button>
        <el-button icon="el-icon-connection" size="mini"  @click="summaryShow" v-if="isShow">统计汇总关闭</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rechargeOrderList" border ref="table_show" :show-summary="true" :summary-method="getSummaries">
      <el-table-column label="充值订单号" align="center" prop="rechargeOrderId" width="220">
        <template slot-scope="scope" class="pre-text">
          <!-- <span style="color:#20B2AA;cursor: pointer;" 
                @click="queryDetails(scope.row)">{{scope.row.rechargeOrderId}}</span> -->
                <span style="color:#20B2AA;cursor: pointer;">{{scope.row.rechargeOrderId}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户手机" align="center" prop="userPhone">
        <template slot-scope="scope" class="pre-text">
          <!-- <span style="color:#20B2AA;cursor: pointer;"
                @click="toUserInfo(scope.row)">{{ scope.row.userPhone.substr(0, 3) + '****' + scope.row.userPhone.substr(-4) }}</span> -->
                <span style="color:#20B2AA;cursor: pointer;">{{ scope.row.userPhone.substr(0, 3) + '****' + scope.row.userPhone.substr(-4) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="用户昵称" align="center" prop="userName" /> -->
      <el-table-column label="充值金额(元)" align="center" prop="rechargeAmount" />
      <el-table-column label="到账金额(元)" align="center" prop="rechargeIntoAmount" />
      <el-table-column label="支付状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_status" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="支付类型" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_type" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <el-table :data="summaryData" v-loading="loading" border v-show="isShow">
      <el-table-column label="充值金额总计(元)" prop="allPrice"></el-table-column>
      <el-table-column label="到账金额总计(元)" prop="allBalance"></el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer
      :visible.sync="open"
      size="45%"
      class="drawer-normal"
      :before-close="handleClose">
      <div class="content" style="padding:0 20px">
        <el-descriptions class="margin-top" title="充值订单详情" :column="1" :size="size" border>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              用户名:
            </template>
            {{ form.userName }}
          </el-descriptions-item>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              订单号:
            </template>
            {{ form.rechargeOrderId }}
          </el-descriptions-item>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              充值金额:
            </template>
            {{ form.rechargeAmount }}元

          </el-descriptions-item>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              到账金额:
            </template>
            {{ form.rechargeIntoAmount }}元
          </el-descriptions-item>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              创建时间:
            </template>
            {{ parseTime(form.createTime, "{y}-{m}-{d} {h}:{i}:{s}") }}
          </el-descriptions-item>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              支付时间:
            </template>
            {{ parseTime(form.payTime, "{y}-{m}-{d} {h}:{i}:{s}") }}
          </el-descriptions-item>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              充值渠道:
            </template>
            <dict-tag :options="dict.type.pay_type" :value="form.payType"/>
          </el-descriptions-item>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              订单状态:
            </template>
            <dict-tag :options="dict.type.order_status" :value="form.orderStatus"/>
          </el-descriptions-item>
          <el-descriptions-item label-class-name="my-label">
            <template slot="label">
              支付状态:
            </template>
            <dict-tag :options="dict.type.pay_status" :value="form.payStatus"/>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-drawer>
    <e-user-info ref="userInfo"/>
  </div>
</template>

<script>
import { listRechargeOrder, getRechargeOrder,totalPrice } from "@/api/zq/rechargeOrder";
import eUserInfo from '@/components/userInfo/index.vue';
export default {
  name: "RechargeOrder",
  components: {
    eUserInfo
  },
  dicts: ['pay_status', 'pay_type', 'order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      size: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 是否显示汇总
			isShow: false,
      userPhone:"",
      // 总条数
      total: 0,
      // 余额充值订单表格数据
      rechargeOrderList: [],
      summaryData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userPhone: null,
        userName: null,
        rechargeAmount: null,
        rechargeIntoAmount: null,
        payStatus: null,
        payType: null,
        orderStatus: null,
        payTime: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询余额充值订单列表 */
    getList() {
      this.loading = true;
      listRechargeOrder(this.queryParams).then(response => {
        this.rechargeOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      totalPrice(this.queryParams).then(response => {
        this.summaryData = [];
        this.summaryData = this.summaryData.concat(response.data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
          rechargeOrderId: null,
          userPhone: null,
          userName: null,
          rechargeAmount: null,
          rechargeIntoAmount: null,
          payStatus: null,
          payType: null,
          orderStatus: null,
          payTime: null,
          createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rechargeOrderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleClose() {
        this.open = false
      },
      // 用户详情
    toUserInfo(row) {
        this.$refs.userInfo.getData(row.userPhone)
    },
    summaryShow() {
        if (this.isShow) {
          this.isShow = false
        } else {
          this.isShow = true
        }
      },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加余额充值订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rechargeOrderId = row.rechargeOrderId || this.ids
      getRechargeOrder(rechargeOrderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改余额充值订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rechargeOrderId != null) {
            updateRechargeOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRechargeOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    getSummaries(param) {
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '汇总';
            return;
          }
          // 需要计算汇总的字段
          const includingOptions = [`rechargeAmount`,`rechargeIntoAmount`]
          const values = data.map(item => Number(item[column.property]));
          if (includingOptions.includes(column.property)) {
            if (!values.every(value => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                } else {
                  return prev;
                }
              }, 0);
              sums[index] = sums[index].toFixed(2);
              if ([`balance`].includes(column.property)) {
                sums[index] = sums[index]/100 + '元';
              } else{
				  sums[index] = sums[index] + '元';
			  }
            } else {
              sums[index] = '';
            }
          } else {
            return "";
          }
        });
        return sums;
      },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rechargeOrderIds = row.rechargeOrderId || this.ids;
      this.$modal.confirm('是否确认删除余额充值订单编号为"' + rechargeOrderIds + '"的数据项？').then(function() {
        return delRechargeOrder(rechargeOrderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/rechargeOrder/export', {
        ...this.queryParams
      }, `rechargeOrder_${new Date().getTime()}.xlsx`)
    },
    queryDetails(row) {
        this.reset();
        const rechargeOrderId = row.rechargeOrderId || this.ids;
        getRechargeOrder(rechargeOrderId).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "充值订单详情";
        });
      },
  }
};
</script>
